package com.dogborther.pojo;

import com.dogborther.utils.ChopstickStatus;
import com.dogborther.utils.PhilosopherStatus;
import com.dogborther.utils.TimeConstant;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.net.URL;
import java.util.Random;

import static com.dogborther.utils.TimeConstant.getRandomThinkTime;

@NoArgsConstructor
@Data
public class Mode0Philosopher extends Philosopher {
    private int id;
    private Button button;
    private ImageView imageView;
    private ImageView leftHandChopstick;
    private ImageView rightHandChopstick;
    private Chopstick leftChopstick;
    private Chopstick rightChopstick;
    public void setImage(PhilosopherStatus philosopherStatus) {
        URL resource = Chopstick.class.getResource(philosopherStatus.getImagePath());
        Image image = new Image(resource.toExternalForm(), 200, 200, true, true, true);
        imageView.setImage(image);
    }
    public void run() {
        System.out.println("mode0：开始");
        think();
        hungry();
        takeChopstickFromLeftToRight();
        dropDownChopsticks();
    }
    public void think() {
        this.setImage(PhilosopherStatus.THINKING);
        try {
            Thread.sleep(getRandomThinkTime());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public void eat() {
        this.setImage(PhilosopherStatus.EATING);
    }
    public void hungry() {
        this.setImage(PhilosopherStatus.HUNGRY);
    }
    public void takeChopstickFromLeftToRight() {
        synchronized (leftChopstick) {
            leftChopstick.setImage(ChopstickStatus.TAKE_UP);
            leftChopstick.getImageView().setVisible(false);
            leftHandChopstick.setVisible(true);
            try {
                Thread.sleep(TimeConstant.WAIT_NEXT_CHOPSTICK_TIME);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (rightChopstick) {
                rightChopstick.setImage(ChopstickStatus.TAKE_UP);
                rightChopstick.getImageView().setVisible(false);
                rightHandChopstick.setVisible(true);
                eat();
                try {
                    Thread.sleep(TimeConstant.getRandomEatingTime());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                this.setImage(PhilosopherStatus.THINKING);
            }
        }
    }
    public void takeChopstickFromRightToLeft() {
        synchronized (rightChopstick) {
            rightChopstick.setImage(ChopstickStatus.TAKE_UP);
            rightChopstick.getImageView().setVisible(false);
            rightHandChopstick.setVisible(true);
            try {
                Thread.sleep(TimeConstant.WAIT_NEXT_CHOPSTICK_TIME);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (leftChopstick) {
                leftChopstick.setImage(ChopstickStatus.TAKE_UP);
                leftChopstick.getImageView().setVisible(false);
                leftHandChopstick.setVisible(true);
                eat();
                try {
                    Thread.sleep(TimeConstant.getRandomEatingTime());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                this.setImage(PhilosopherStatus.THINKING);
            }
        }
    }
    private synchronized void dropDownChopsticks() {
        this.leftHandChopstick.setVisible(false);
        this.rightHandChopstick.setVisible(false);
        this.leftChopstick.getImageView().setVisible(true);
        this.rightChopstick.getImageView().setVisible(true);
        leftChopstick.setImage(ChopstickStatus.DROP_DOWN);
        rightChopstick.setImage(ChopstickStatus.DROP_DOWN);
    }
}
